package uk.ac.ebi.dod.beans;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.faces.event.ValueChangeEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Florian Reisinger
 * @since 0.1
 */
public class InputSource {

    private static final Log log = LogFactory.getLog(InputSource.class);

    String type;
    String sourceDBName;
    String sourceLocation;
    List<Filter> filters;

//    private String filter;
    private String filterLabel;
    private Filter selectedFilter;
    private String filterParam;
    private boolean enableFilterParamInput;

    ///// ///// ///// ///// /////
    // CONSTRUCTOR

    public InputSource() {
        this.filters = null;
        this.sourceDBName = null;
        this.sourceLocation = null;
        this.type = null;
        this.filterLabel = null;
        this.filterParam = null;
    }

    ///// ///// ///// ///// /////
    // METHODS

    public void reset() {
        this.filters = null;
        this.selectedFilter = null;
        this.filterParam = null;
        this.filterLabel = null;
        this.enableFilterParamInput = true;
    }

    public int getFilterSize() {
        if (filters != null) {
            return filters.size();
        } else {
            return 0;
        }
    }

    public void addFilter(javax.faces.event.ActionEvent e) {
        // ToDo: initReport();

        if (filters == null) {
            filters = new ArrayList<Filter>();
            log.debug("Created new filter list: " + filters + " while adding Filter: " + filterLabel);
        }
        Filter f = new Filter();
        f.setParent(filters);
        f.setLabel(filterLabel);

        f.setName(selectedFilter.getName());
        f.setLabel(selectedFilter.getLabel());
        if (selectedFilter.getName().equalsIgnoreCase(ConfigCollector.FILTER_NAME_MAT)) {
            // we don't take filter parameters into account for this filter!
        } else {
            f.setParameter(filterParam);
        }
        f.setEnableParamInput(selectedFilter.getEnableParamInput());
        filters.add(f);
        log.debug("Added filter [" + f.getName() + "|" + f.getParameter() + "] to filter list: " + filters);
        selectedFilter = null;
        filterParam = null;
    }

    public void renderFilterParamInput(ValueChangeEvent vce) {
        Object o = vce.getNewValue();
        if (o instanceof Filter) {
            Filter filter = (Filter) o;
            this.enableFilterParamInput = filter.getEnableParamInput();
            this.filterParam = null;
        } else {
            log.error("ValueChangeEvent on filter selection produced something other than a Filter object! Ignoring.");
        }
    }

    ///// ///// ///// ///// /////
    // GETTER & SETTER

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getSourceDBName() {
        return sourceDBName;
    }

    public void setSourceDBName(String sourceDBName) {
        this.sourceDBName = sourceDBName;
    }

    public String getSourceLocation() {
        return sourceLocation;
    }

    public void setSourceLocation(String sourceLocation) {
        this.sourceLocation = sourceLocation;
    }

    public List<Filter> getFilters() {
        return filters;
    }

    public void setFilters(List<Filter> filters) {
        this.filters = filters;
    }

    public Filter getSelectedFilter() {
        return selectedFilter;
    }

    public void setSelectedFilter(Filter selectedFilter) {
        this.selectedFilter = selectedFilter;
    }

    public String getFilterParam() {
        return filterParam;
    }

    public void setFilterParam(String filterParam) {
        this.filterParam = filterParam;
    }

    public boolean getEnableFilterParamInput() {
        return enableFilterParamInput;
    }

    public void setEnableFilterParamInput(boolean enableFilerParamInput) {
        this.enableFilterParamInput = enableFilerParamInput;
    }
}
